<%@page import="java.sql.Connection"%>
<%@page import="com.mysql.jdbc.Driver"%>
<%@page import="java.sql.DriverManager"%>
<%@page import="java.sql.Statement"%>

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%
String usermsg = null;
String pw1msg = null;
String pw2msg = null;
String emailmsg = null;
String userid = null;
String pw1 = null;
String pw2 = null;
String email = null;

if (request.getParameter("userid") != null){
	userid = request.getParameter("userid");
	pw1 = request.getParameter("pw1");
	pw2 = request.getParameter("pw2");
	email = request.getParameter("email");
	
	boolean valid = true;
	if (userid.length() < 4 || userid.length() >= 20) {
		valid = false;
		usermsg = "帳號長度不符合!!";
	}
	if (!pw1.equals(pw2)) {
		valid = false;
		pw1msg = "密碼必要相同!!";
		pw2msg = "密碼必要相同!!";
	}
	if (email.indexOf("@") == -1){
		valid = false;
		emailmsg = "電子郵件帳號長度不符合!!";
	}
	
	if (valid) {
		// 1
		DriverManager.registerDriver(new Driver());
		// 2
		Connection conn = DriverManager.getConnection(
				"jdbc:mysql://j.snpy.org/j102", "jstu", "abc123");
		Statement stmt = conn.createStatement();
		String sql = 
			"INSERT INTO users(id, password, email) values ('" + userid + "', '" + pw1 + "', '" + email + "' )";
		System.out.println(sql);
		int rowcount = stmt.executeUpdate(sql);
		System.out.println(rowcount);
	}
}
%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>

<form action="">
		帳號:<input id="userid" name="userid" type="text" value="<%=userid==null? "": userid%>" /><%=usermsg==null? "": usermsg %>
		<br/>
		密碼:<input id="pw1" name="pw1" type="text" value="<%=pw1==null? "": pw1%>" /><%=pw1msg==null? "": pw1msg %>
		<br/>
		密碼:<input id="pw2" name="pw2" type="text" value="<%=pw2==null? "": pw2%>" /><%=pw2msg==null? "": pw2msg %>
		<br/>
		E-mail:<input id="email" name="email" type="text" value="<%=email==null? "": email%>" /><%=emailmsg==null? "": emailmsg %>
		<br/>
		<input type="submit" value="註冊"/>
	</form> 
</body>
</html>